package com.ruoyi.framework.web.service.app;

import com.ruoyi.common.core.domain.entity.H5User;
import com.ruoyi.common.core.domain.model.H5LoginUser;
import com.ruoyi.system.service.IH5UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class H5UserDetailsService implements UserDetailsService {
    @Autowired
    private IH5UserService h5UserService; // 自定义 H5 用户服务接口

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        H5User user = h5UserService.selectUserByPhoneNumber(username);
        if (user == null) {
            throw new UsernameNotFoundException("H5用户不存在");
        }
        if ("1".equals(user.getStatus())) { // 假设 1 为正常状态
            throw new DisabledException("H5用户已禁用");
        }
        return new H5LoginUser(user);
    }
}